*********************************************************************************
/*    Create waterfall chart unfolding total cost and revenue per audit	 		*/
*********************************************************************************
* File paths
local datapath "${mvpf_audits}/data/cleaned" 
local graphpath "${graphpath}" 

* Waterfall chart to produce
local useloop = `1' // =1 for operational, =2 corr, =3 both

* Ops waterfall
	if `useloop'== 1 { 
		local hetlist `""if exam_type_pooled==1" "'
		local hettexts `" "Operational" "'
		local hetlabs `""ops" "'
	}

* Corr waterfall
	if `useloop'== 2 { 
		local hetlist `""if exam_type_pooled==3" "'
		local hettexts `" "Correspondence" "'
		local hetlabs `""corr" "'
	}
	
* create waterfall for all audits
	if `useloop'== 3 { 
		local hetlist `""if exam_type_pooled==1" "if exam_type_pooled==3""'
		local hettexts `" "Operational" "Correspondence""'
		local hetlabs `""ops" "corr""'
	}	
	
/*------------------------------------------------------------------------------
Create waterfall 
------------------------------------------------------------------------------*/
local j=1
foreach h in `hetlist' {
	local hetlab: word `j' of `hetlabs'
	local hettext: word `j' of `hettexts'
	di "`h'" "`hetlab'"	
	
		if "`hetlab'"== "ops" {
		local lb = 0
		local ub = 15000 // round(`rev_tot_`hetlab''*1.13, 10) 
		local int = 3000 //(`ub' - `lb')/5
		}
			
		if "`hetlab'"== "corr" {
		local lb = 0
		local ub = 1200 // round(`rev_tot_`hetlab''*1.13, 10) 
		local int = 300 //(`ub' - `lb')/5
		}

		local max_obs = 12 // Total number of components 
		local white_out_first_cost = 2 // Observation first white out bar appears 
		local white_out_last_cost = 4 // Observation last white out bar appears 
		local xaxis_total_cost = 5
			
		local white_out_first = 8 // Observation first white out bar appears 
		local white_out_last = 11 // Observation last white out bar appears 
		local xaxis_total_rev = 12

		* set y-axis label and figure name
		local ylab = "Dollars per Audit ($)"
		local fig_name = "waterfall_costs_rev"
			
		* y coordinate for text
		local y = (7/8)*`ub'
		
		* Exam overhead multipliers breakdown 
		if "`hetlab'"== "ops" {
		    local m1 = 1.9279
			local m2 = 1.0054
			local m3 = 1.4521  
			
			local scale = 650
		}
		
		if "`hetlab'"== "corr" {
		    local m1 = 6.5718
			local m2 = 3.4308
			local m3 = 6.0677
			
			local scale = 10
		}
		
		global labs "mlabs(small) msym(none) mlabc("$tc")"
		* Text for slides
		if "${version}" == "paper" {
			global text = ""
		}

		global oh_color = "240 128 128"
		if `useloop' == 1 {
			use "${mvpf_audits}/data/figures_rep/fig_1.dta", clear
		}
		
		if `useloop' == 2 {
			use "${mvpf_audits}/data/figures_rep/ap2.dta", clear
		}
			* Generate chart 
		tw  (bar cost xaxis if inrange(xaxis, 4, 6), barwidth(0.6)  lcolor(%0) color(gs11))  ///
			(bar cost2 xaxis if inrange(xaxis, 0, 3), barwidth(0.6)  lcolor(cranberry) color("$oh_color"))  ///
			(bar costs_labor2 xaxis if xaxis<1 | xaxis==2 | xaxis==3, barwidth(0.6)  lcolor("$oh_color") color("$oh_color")) ///
			(bar rev xaxis if xaxis>=`xaxis_total_rev', barwidth(0.6)  lcolor(%0) color(gs11))  ///
			(bar rev2 xaxis if xaxis<`xaxis_total_rev', barwidth(0.6)  lcolor(%0) color(sea))  ///
			(bar oh_m3 xaxis, barwidth(0.6)  lcolor(cranberry) color("$oh_color"))  ///
			(bar oh_m2 xaxis, barwidth(0.6)  lcolor(cranberry) color("$oh_color"))  ///
			(bar oh_m1 xaxis, barwidth(0.6)  lcolor(cranberry) color("$oh_color"))  ///
			(bar costs_labor2 xaxis, barwidth(0.6)  lcolor(cranberry) color(cranberry))  ///
			(bar cost2 xaxis if xaxis==4, barwidth(0.6)  lcolor(cranberry) color(cranberry))  ///
			(bar white_out xaxis if inrange(xaxis, `white_out_first', `white_out_last') | ///
				inrange(xaxis, `white_out_first_cost', `white_out_last_cost'), ///
				color(white) lcolor(white) lw(thin) barwidth(0.65)) ///
			(scatter cost xaxis if xaxis>=`xaxis_total_cost' & xaxis<`max_obs', mlab(label_cost) $labs mlabpos(12)) ///
			(scatter cost2 xaxis if xaxis<`xaxis_total_cost', mlab(label_cost) $labs mlabpos(12)) ///
			(scatter rev xaxis if xaxis>=`xaxis_total_rev', mlab(label_rev) mlabs(small) msym(none) mlabc("$tc") mlabpos(12)) ///
			(scatter rev2 xaxis if inrange(xaxis, 6, 10), mlab(label_rev) mlabs(small) msym(none) mlabc("$tc") mlabpos(12)) ///
			(scatter white_out xaxis if xaxis==11, mlab(label_rev) mlabs(small) msym(none) mlabc("$tc") mlabpos(6)) ///
			///(pcarrowi `y' 2.4 `y' 2.9, color(black) ) ///
			, ///
			ylabel(`lb'(`int')`ub', nogrid format(%9.0fc) ) yscale(r(`lb_f' `ub_f') ) ///
			ytitle("`ylab'", size(4.5)) xtitle("")   ///
			legend(off) ///
			${title} ${axis_size} ///
			xlabel( ///
				0.99 `"Exams"' ///
				2 `""Appeals" """' ///
				3 `"Collections"' ///
				4 `"Projected"' ///
				5 `""{bf:Total}" "{bf:costs}""' ///
				6 `" "' ///
				7 `"Exams"' ///
				8 `"Appeals"' ///
				9 `"Collections"' ///
				10 `"Projected"' ///
				11 `""Time" "discount""' ///
				12 `""{bf:Total}" "{bf:revenue}""' ///
				, labsize(2) notick nogrid )  
			graph export "`graphpath'/`fig_name'_`hetlab'.wmf", as(wmf) replace

// 			restore

		local j=`j'+1
	}		